package com.sport.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.sport.entity.SysUser;
import com.sport.entity.vo.Login;
import com.sport.model.Result;
import com.sport.service.SysUserService;
import com.sport.utils.PasswordUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@RestController
@Tag(name = "用户登录管理")
@ApiSupport(author = "开心摆烂", order = 1)
@RequestMapping("/login")
public class LoginController {

    @Resource
    private SysUserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @SaIgnore
    @PostMapping("/username")
    @Operation(summary = "通用登录接口")
    public Result<Map<String, String>> login(@RequestBody Login login) {
        SysUser user = null;
        if (StringUtils.isNotBlank(login.getUsername())) {
            user = userService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, login.getUsername()));
        } else if (StringUtils.isNotBlank(login.getEmail())) {
            user = userService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getEmail, login.getEmail()));
        } else if (StringUtils.isNotBlank(login.getPhone())) {
            user = userService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getPhone, login.getPhone()));
        } else {
            return Result.error("请输入有效的登录信息");
        }

        if (user == null) {
            return Result.error("用户名/邮箱/手机号或密码错误");
        }

        // 密码校验
        String password = login.getPassword();
        String slat = user.getSlat();
        String decrypt = PasswordUtils.decrypt(slat, user.getPassword());
        if (password.equals(decrypt)) {
            StpUtil.login(user.getId(), "pc");
            Map<String, String> map = new HashMap<>();
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            map.put("tokenHead", tokenInfo.tokenName);
            map.put("token", tokenInfo.tokenValue);
            return Result.success(map);
        }
        return Result.error("用户名/邮箱/手机号或密码错误");
    }

    @GetMapping("/logout")
    @Operation(summary = "退出登录")
    public Result<Boolean> loginout() {
        StpUtil.logout();
        return Result.success();
    }


    @PostMapping("/test")
    @SaCheckPermission("login:test")
    public SaResult test() {
        return SaResult.ok();
    }
}
